package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.DriveLoginLogQueryDto;
import com.kaili.basic.pojo.dto.SysLogQueryDto;
import com.kaili.basic.pojo.dto.SysLoginLogQueryDto;
import com.kaili.basic.pojo.entity.DriveLoginLogEntity;
import com.kaili.basic.pojo.entity.SysLoginLogEntity;
import com.kaili.basic.pojo.vo.SysLogExportVO;
import com.kaili.basic.service.BaseLogService;
import com.kaili.common.pojo.entity.SysLogEntity;
import com.kaili.common.response.RestStatus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by renzhikun on 18-6-14.
 */
@Service
public class BaseLogServiceImpl  implements BaseLogService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<SysLoginLogEntity> querySysLoginLog(SysLoginLogQueryDto dto,String collectionName) {
        Page<SysLoginLogEntity> page=new Page<>(dto.getPage(),dto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(dto.getPage()==1?0:dto.getPage(),dto.getPageSize());
        //2.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"loginTime");
        //3.过滤参数
        Criteria criteria =new Criteria();
        setSysLoginLogWhere(criteria,dto);

        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, SysLoginLogEntity.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,SysLoginLogEntity.class, collectionName));
        page.setTotal(count);
        return page;
    }


    @Override
    public Page<SysLogEntity> querySysLog(String collectionName,SysLogQueryDto dto) {
        Page<SysLogEntity> page=new Page<>(dto.getPage(),dto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(dto.getPage()==1?0:dto.getPage(),dto.getPageSize());
        //2.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"operatorTime");
        //3.过滤参数
        Criteria criteria =new Criteria();
        setSysLogWhere(criteria,dto);
        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, SysLogEntity.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,SysLogEntity.class, collectionName));
        page.setTotal(count);
        return page;
    }

    @Override
    public List<SysLogExportVO> querySysLogExport(String collectionName, SysLogQueryDto dto) {
        //1.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"operatorTime");
        //2.过滤参数
        Criteria criteria =new Criteria();
        setSysLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,SysLogExportVO.class, collectionName);
    }

    @Override
    public SysLoginLogEntity queryLogOne(String username, String collectionName) {
        Query query=new Query(Criteria.where("userName").is(username));
        query.with(new Sort(Sort.Direction.DESC,"loginTime")).with(new PageRequest(0,1));
        List<SysLoginLogEntity> list = mongoTemplate.find(query, SysLoginLogEntity.class, collectionName);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public void saveSysLoginLog(SysLoginLogEntity logBean, String collectionName) {
        mongoTemplate.save(logBean,collectionName);
    }

    @Override
    public List<SysLoginLogEntity> querySysLogExport(SysLoginLogQueryDto dto, String collectionName) {
        //1.时间倒序
        Sort sort=new Sort(Sort.Direction.DESC,"loginTime");
        //2.过滤参数
        Criteria criteria =new Criteria();
        setSysLoginLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,SysLoginLogEntity.class, collectionName);
    }

    @Override
    public void saveDriveLogs(DriveLoginLogEntity entity, String collectionName) {
        mongoTemplate.save(entity,collectionName);
    }

    @Override
    public Page<DriveLoginLogEntity> queryDriveLogs(String collectionName, DriveLoginLogQueryDto queryDto) {
        Page<DriveLoginLogEntity> page=new Page<>(queryDto.getPage(),queryDto.getPageSize());
        //1.设置分页
        Pageable pageable=new PageRequest(queryDto.getPage()==1?0:queryDto.getPage(),queryDto.getPageSize());
        //1.时间倒序
        Sort sort=null;
        if(0==queryDto.getTimeType()){
            sort=new Sort(Sort.Direction.DESC,"loginTime");
        }else{
            sort=new Sort(Sort.Direction.DESC,"registerTime");
        }
        //3.过滤参数
        Criteria criteria =new Criteria();
        setDriveLoginLogWhere(criteria,queryDto);
        //4.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        long count = mongoTemplate.count(query, DriveLoginLogEntity.class, collectionName);
        query.with(pageable);
        page.setRecords(mongoTemplate.find(query,DriveLoginLogEntity.class, collectionName));
        page.setTotal(count);
        return page;
    }

    @Override
    public List<DriveLoginLogEntity> queryDriveLogExport(DriveLoginLogQueryDto dto, String collectionName) {
        //1.时间倒序
        Sort sort=null;
        if(0==dto.getTimeType()){
            sort=new Sort(Sort.Direction.DESC,"loginTime");
        }else{
            sort=new Sort(Sort.Direction.DESC,"registerTime");
        }
        //2.过滤参数
        Criteria criteria =new Criteria();
        setDriveLoginLogWhere(criteria,dto);
        //3.组合查询条件
        Query query=new Query(criteria);
        query.with(sort);
        return mongoTemplate.find(query,DriveLoginLogEntity.class, collectionName);
    }


    public void setSysLogWhere(Criteria criteria,SysLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorName())){
            criteria.and("operatorName").regex(dto.getOperatorName());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorModule())){
            criteria.and("operatorModule").regex(dto.getOperatorModule());
        }
        if(StringUtils.isNoneBlank(dto.getOperatorType())){
            criteria.and("operatorType").is(dto.getOperatorType());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            criteria.and("operatorTime").gte(dto.getStartTime()).lte(dto.getEndTime());
        }
    }

    public void setSysLoginLogWhere(Criteria criteria,SysLoginLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getName())){
            criteria.and("name").regex(dto.getName());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            criteria.and("loginTime").gte(dto.getStartTime()).lte(dto.getEndTime());
        }
    }

    public void setDriveLoginLogWhere(Criteria criteria,DriveLoginLogQueryDto dto){
        if(StringUtils.isNoneBlank(dto.getOrgan())){
            criteria.and("organ").regex(dto.getOrgan());
        }
        if(StringUtils.isNoneBlank(dto.getName())){
            criteria.and("name").regex(dto.getName());
        }
        if(dto.getStartTime()!=null&&dto.getEndTime()!=null){
            if(0==dto.getTimeType()){
                criteria.and("loginTime").gte(dto.getStartTime()).gte(dto.getEndTime());
            }else{
                criteria.and("registerTime").gte(dto.getStartTime()).gte(dto.getEndTime());
            }
        }
    }

}
